# **************************************
# --*-- coding: utf-8 --*--
# @Time    : 2024-11-04
# @Author  : white
# @FileName: notepad.py
# @Software: PyCharm
# **************************************
import winreg
from tkinter import Button, Label, Entry, \
    Variable, DISABLED
from .option_reg import Reg
from .base import Base
from re import search
from comon import logger


class Notepad(Base):
    def __init__(self, ui):
        super().__init__()
        self.ui = ui
        self.left_frame = self.ui.left_frame
        self.right_frame = self.ui.right_frame
        self.name = "修改记事本背景色"

        self.key = "Window"
        self.data_type = winreg.REG_SZ
        self.reg_path = "HKEY_CURRENT_USER\Control Panel\Colors"
        self.reg_obj = Reg(self.reg_path)
        self.hobby_color = (202, 234, 206)
        self.default_color = (255, 255, 255)

        self.ui_init()

    def reg(self, R: int = None, G: int = None, B: int = None):
        if R is None and G is None and B is None:
            color = "255 255 255"
        else:
            color = f"{R} {G} {B}"

        self.reg_obj.create_or_change_key(self.key, color, self.data_type)

    def ui_init(self):
        self._button()
        self.button.bind("<Button-1>", self.option)

    def _button(self):
        self.button = Button(self.left_frame, text=self.name)
        self.button.pack(pady=self.ui.pady_upper, anchor="w")

    @Base.clear_frame
    def option(self, event=None):
        if self.button['state'] == DISABLED:
            return
        Label(self.right_frame, text="修改颜色").grid(row=0, column=0, sticky="w", pady=self.ui.pady_upper)
        self.input_color = Variable()
        Entry(self.right_frame, textvariable=self.input_color, width=15) \
            .grid(row=0, column=1, sticky="w", pady=self.ui.pady_upper)
        self.color_boat_button = Button(self.right_frame, text="色板", command=self._color_boat)
        self.color_boat_button.grid(row=0, column=2, sticky="w", pady=self.ui.pady_upper)

        Button(self.right_frame, text="爱好颜色", command=lambda : self.submit(color=self.hobby_color)) \
            .grid(row=1, column=0, sticky="w", pady=self.ui.pady_upper)
        Button(self.right_frame, text="恢复默认颜色", command=lambda : self.submit(color=self.default_color)) \
            .grid(row=2, column=0, sticky="w", pady=self.ui.pady_upper)
        Button(self.right_frame, text="修改", command=self.submit) \
            .grid(row=3, column=0, sticky="w", pady=self.ui.pady_upper)

    def _color_boat(self, event=None):
        if self.color_boat_button['state'] == DISABLED:
            return
        color = self.choose_color()
        if not color[0]:
            return
        rgb = color[0]
        self.input_color.set(f"{rgb[0]},{rgb[1]},{rgb[2]}")

    def submit(self, event=None, color: tuple = None):
        def f():
            try:
                self.reg(*tuple(color))
                msg = f"{color} 记事本背景色修改生成，重启电脑后生效！"
                self.ui.show_msg(msg)
                logger.info(msg)
            except Exception as e:
                logger.error(e)

        rgb = self.input_color.get().strip()
        if color is not None:
            f()
            return

        if not len(rgb) > 0:
            msg = "颜色值无效！eg: R,G,B"
            self.ui.show_msg(msg, "warning")
            logger.info(msg)
            return
        try:
            rgb = search("(\d+),(\d+),(\d+)", rgb).groups()
            logger.info(f"匹配到颜色：{rgb}")
        except Exception as e:
            self.ui.show_msg(str(e), "error")
            logger.error(e)
            return
        f()
